package com.qfedu.hotelssm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.qfedu.hotelssm.dao.SalaryDao;
import com.qfedu.hotelssm.dto.DepSexSalaryDto;
import com.qfedu.hotelssm.dto.SumSalaryDto;
import com.qfedu.hotelssm.entity.Property;
import com.qfedu.hotelssm.entity.Salary;
import com.qfedu.hotelssm.service.intf.SalaryService;
import com.qfedu.hotelssm.util.StringUtil;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

@Service
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    private SalaryDao dao;

    @Override
    public R save(Salary salary) {
        //计算总工资
        if (salary.getBasepay() != null || salary.getMeritpay() != null) {
            double sum = salary.getBasepay() + salary.getMeritpay();
            salary.setSalary(sum);
        }
        if (dao.save(salary) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if (dao.delete(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(Salary salary) {
        //计算总工资
        if (salary.getBasepay() != null || salary.getMeritpay() != null) {
            double sum = salary.getBasepay() + salary.getMeritpay();
            salary.setSalary(sum);
        }
        if (dao.update(salary) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public PageBean selectPage(String name, int page, int limit) {
        if (StringUtil.isNotEmpty(name)) {
            name = "%" + name + "%";
        }
        int start = (page - 1) * limit;

        return new PageBean((int) dao.selectCount(name), dao.selectPage(name, start, limit));
    }

    @Override
    public R deleteByIds(int[] ids) {
        if (ids != null) {
            if (dao.deleteByIds(ids) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R all() {
        if (dao.all() != null){
            return R.ok(dao.all());
        }else{
            return R.fail();
        }
    }

    @Override
    public List<Salary> download() {
        return dao.all();
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
        if (!file.isEmpty()){
            List<Salary> list = EasyExcel.read(file.getInputStream(), Salary.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {}
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
            }).sheet().doReadSync();
            for (Salary salary : list) {
                //计算总工资
                if (salary.getBasepay() != null || salary.getMeritpay() != null) {
                    double sum = salary.getBasepay() + salary.getMeritpay();
                    salary.setSalary(sum);
                }
            }
            if (dao.saveBatch(list) > 0) {
                R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R allDto() {
        if (dao.allDto() != null) {
            return R.ok(dao.allDto());
        } else {
            return R.fail();
        }
    }

    @Override
    public R sumSalary() {
        if (dao.sumSalary() != null) {
            List<SumSalaryDto> list = new ArrayList<>();
            List<SumSalaryDto> sumDto = dao.sumSalary();
            sumDto.forEach((c) -> {
                list.add(new SumSalaryDto(c.getValue(), c.getName()));
            });
            return R.ok(list);
        } else {
            return R.fail();
        }
    }

    @Override
    public R sumSexSalary() {
        List<DepSexSalaryDto> list = dao.sumSexSalary();
        ArrayList<Double> nums = new ArrayList<>();
        ArrayList<String> names = new ArrayList<>();
        if (list != null) {
            for (DepSexSalaryDto obj : list) {
                if (obj.getSex() == 1){
                    names.add(obj.getName() + "男");
                    if (obj.getSum() == null){
                        nums.add(1.0);
                    }else{
                        nums.add(obj.getSum());
                    }
                }else if(obj.getSex() == 2){
                    names.add(obj.getName() + "女");
                    if (obj.getSum() == null){
                        nums.add(1.0);
                    }else{
                        nums.add(obj.getSum());
                    }
                }
            }
            List<Object> list1 = new ArrayList<Object>();
            list1.add(names);
            list1.add(nums);
            return R.ok(list1);
        } else {
            return R.fail();
        }
    }
}
